# 导入图片资源
from imageResource import imageResource


# 编写爆炸类
class Boom:
    # 爆炸图片元组
    imgs = (imageResource.getBoomImg1, imageResource.getBoomImg2, imageResource.getBoomImg3)
    def __init__(self, x, y):
        # 爆炸的大小
        self.size = 0
        # 设置爆炸的飞行速度
        self.speed = 0
        # 爆炸的位置
        self.x = x
        self.y = y
        # tkinter 读取图片文件
        self.img = imageResource.getBoomImg1()
        # 爆炸的id
        self.id = None
        # 当前爆炸是否死亡的标志 - 死亡 True - 未死亡 False
        self.isDie = 15

    # 爆炸移动的行为
    def move(self, keys=None):
        if self.isDie // 5:
            self.img = Boom.imgs[3 - (self.isDie // 5)]()
            self.isDie -= 1

    # 显示的方法
    def show(self, canvas):
        canvas.show(self)

    # 是否需要清除
    def isClear(self, winWidth, winHeight):
        return not (self.isDie // 5)
    
    # 是否产生碰撞的逻辑 - 如果碰撞 返回True 如果不碰撞 返回False
    def isCollision(self, obj):
        Asize = self.size // 2
        Bsize = obj.size // 2
        Ax = self.x - Asize // 2
        Ay = self.y - Asize // 2
        Bx = obj.x - Bsize // 2
        By = obj.y - Bsize // 2

        t1 = Ax + Asize < Bx
        t2 = Ax > Bx + Bsize
        t3 = Ay + Asize < By
        t4 = Ay > By + Bsize

        return not (t1 or t2 or t3 or t4)


# 爆炸工厂类
class BoomFactory:
    def __init__(self):
        self.list = []

    # 创建爆炸
    def create(self, x, y):
        obj = Boom(x, y)
        self.list.append(obj)
        return obj
    
    # 清理超出屏幕的爆炸
    def flush(self, winWidth, winHeight):
        i = 0
        while i < len(self.list):
            if self.list[i].isClear(winWidth, winHeight):
                self.list.pop(i)
            else:
                i += 1

boomFactory = BoomFactory()
